package com.sduept.nwld.scriptengine;

import java.io.FileReader;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

/**
 * java调用js
 * @author ldx
 *
 */
public class ExecuteScript {
	public double getGreatCircleDistance(double lat1,double lng1,double lat2,double lng2 ){
		double distanceBetweenTwoTowers =0d;
		Methods executeMethod=initScriptEngine();
		if(executeMethod!=null){
			distanceBetweenTwoTowers=executeMethod.getGreatCircleDistance(lat1, lng1, lat2, lng2);
		}
		return distanceBetweenTwoTowers;
	}
	 
	private  Methods initScriptEngine(){
	  ScriptEngineManager manager = new ScriptEngineManager();
	  ScriptEngine engine = manager.getEngineByName("js");
	  Methods executeMethod=null;
	  try {
		   String path = ExecuteScript.class.getResource("").getPath();
		   engine.eval(new FileReader(path + "JavaScriptMethods.js"));
		   if (engine instanceof Invocable) {
			    Invocable invocable = (Invocable) engine;
			    executeMethod = invocable.getInterface(Methods.class);
		   }
	  }catch (Exception e) {
		  e.printStackTrace();
	  }
	  return executeMethod;
	}
}
